<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/9
 * Time: 下午2:21
 */

namespace app\admin\logic;


use extend\Functions;
use extend\STATUS_CODE;

class Brand extends Base
{
    /**
     * @purpose 新增品牌
     * @param $data
     * @return array
     * @throws \Exception
     */
    public static function create($data): array
    {

        if (!$data['name']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌名称'
            );
        }
        if (!$data['english']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌名称'
            );
        }
        if (!$data['thumb']) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '缺少品牌缩略图'
            );
        }
        $model  = new \app\model\Brand();
        $result = $model->addOne(
            $data['name'],
            $data['english'],
            Functions::pinyin($data['name']),
            $data['thumb']);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '添加成功'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '添加失败'
        );
    }

    /**
     * @purpose 修改品牌
     * @param array $data 需要修改的数据,必须带有id字段
     * @return array
     */
    public static function edit (array $data):array
    {
        if (!isset($data['id']) || empty($data['id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误'
            );
        }
        $model = new \app\model\Brand();
        $result = $model->save($data, ['id' => $data['id']]);
        if ($result !== false) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '更新成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '更新失败'
        );
    }

    /**
     * @purpose 删除品牌
     * @param int $id
     * @param int $uid
     * @return array
     */
    public static function delete (int $id, int $uid) : array
    {
        $model = new \app\model\Brand();
        // todo 如果当前品牌下有商品则不能够删除
        if (
            self::trash($model, ['id' => $id], $uid) &&
            $model->deleteById($id)
        ) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '删除成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '删除失败'
        );
    }

    /**
     * @purpose 对品牌进行排序
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public static function sort (array $data): array
    {
        $model  = new \app\model\Brand();
        $result = $model->saveAll($data);
        if ($result) {
            return Functions::result(
                STATUS_CODE::SUCCESS,
                '排序成功!'
            );
        }
        return Functions::result(
            STATUS_CODE::FAIL,
            '排序失败'
        );
    }

    /**
     * @purpose 获取品牌列表
     * @param $where
     * @param $page
     * @param $size
     * @param string $fields
     * @return array
     */
    public static function items (
        $where,
        $page,
        $size,
        $fields = 'id,name,thumb,english,sort'): array
    {
        $model      = new \app\model\Brand();
        $result     = $model->items(
            $where,
            $page,
            $size,
            'sort asc,create_at desc',
            $fields);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }

    /**
     * @purpose 通过ID获取单条数据信息
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function infoById (int $id):array
    {
        $model      = new \app\model\Brand();
        $fields     = 'id,name,english,thumb';
        $result     = $model->infoById($id, $fields);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }
}